﻿using System;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKRGeometry;

namespace YJKArchMethodLibrary.StructurePoleSuit
{
	public class Edge
	{
		public XYZ StartPoint
		{
			get
			{
				return this.m_StartPoint;
			}
			set
			{
				this.m_StartPoint = value;
			}
		}

		public XYZ EndPoint
		{
			get
			{
				return this.m_EndPoint;
			}
			set
			{
				this.m_EndPoint = value;
			}
		}

		public double Bulge
		{
			get
			{
				return this.m_Bulge;
			}
			set
			{
				this.m_Bulge = value;
			}
		}

		public bool IsArc
		{
			get
			{
				return !Geometry.IsEqual(this.m_Bulge, 0.0);
			}
		}

		public Edge(Edge rhs)
		{
			this.m_StartPoint = rhs.m_StartPoint;
			this.m_EndPoint = rhs.m_EndPoint;
			this.m_Bulge = rhs.m_Bulge;
		}

		public Edge(XYZ startPoint, XYZ endPoint, double bulge)
		{
			this.m_StartPoint = startPoint;
			this.m_EndPoint = endPoint;
			this.m_Bulge = bulge;
		}

		public Curve GetCurve(Autodesk.Revit.ApplicationServices.Application revitApp)
		{
			Curve result;
			if (this.IsArc)
			{
				XYZ ptCenter = new XYZ();
				Geometry.GetCenterWithBulge(this.StartPoint, this.EndPoint, this.Bulge, ref ptCenter);
				XYZ normal = XYZ.BasisZ;
				if (Geometry.LessThan(this.Bulge, 0.0))
				{
					normal = -XYZ.BasisZ;
				}
				XYZ radianPnt = Geometry.CalculatMidPoint(this.StartPoint, this.EndPoint, ptCenter, normal);
				result = revitApp.CreatYJKArc(this.StartPoint, this.EndPoint, radianPnt);
			}
			else
			{
				result = YJKLineEx.YJKGetBound(this.StartPoint, this.EndPoint);
			}
			return result;
		}

		private XYZ m_StartPoint;

		private XYZ m_EndPoint;

		private double m_Bulge;
	}
}
